
package com.rt.schedulebase.core.exception;

import com.rt.schedulenew.utils.util.JsonUtil;
import org.slf4j.LoggerFactory;
import com.rt.schedulenew.utils.global.BusinessException;
import com.rt.schedulenew.utils.jwt.exception.JwtTokenException;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.apache.shiro.authc.AuthenticationException;
import com.rt.schedulenew.utils.global.ErrorCode;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpStatus;
import com.rt.schedulenew.utils.global.JsonResult;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ControllerAdvice;

/**
 * 控制器 全局异常捕获
 */
@ControllerAdvice
@ResponseBody
public class ExceptionAdvice {
    private Logger log = LoggerFactory.getLogger(ExceptionAdvice.class);

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({HttpMessageNotReadableException.class})
    public String handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error("参数解析失败", e);
        JsonResult result = new JsonResult();
        result.setStatus(String.valueOf(HttpStatus.BAD_REQUEST.value()));
        result.setMessage("参数解析失败");
        return JsonUtil.toJson(result);
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    public String handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        log.error("不支持当前请求方法", e);
        JsonResult result = new JsonResult();
        result.setStatus(String.valueOf(HttpStatus.METHOD_NOT_ALLOWED.value()));
        result.setMessage("不支持当前请求方法!");
        return JsonUtil.toJson(result);
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({HttpMediaTypeNotSupportedException.class})
    public String handleHttpMediaTypeNotSupportedException(Exception e) {
        log.error("不支持当前媒体类型", e);
        JsonResult result = new JsonResult();
        result.setStatus(String.valueOf(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value()));
        result.setMessage("不支持当前媒体类型");
        return JsonUtil.toJson(result);
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({AuthenticationException.class})
    public String handleTokenParseException(Exception e) {
        log.error(e.getMessage(), e);
        JsonResult result = new JsonResult();
        result.setStatus(ErrorCode.FAIL.getKey());
        result.setMessage(e.getMessage());
        return JsonUtil.toJson(result);
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({MissingServletRequestParameterException.class})
    public String handleMissingServletRequestParameterException(Exception e) {
        log.error("缺少请求参数,详情：" + e.getMessage(), e);
        JsonResult result = new JsonResult();
        result.setStatus(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()));
        result.setMessage("缺少请求参数,详情：" + e.getMessage());
        return JsonUtil.toJson(result);
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({UncategorizedSQLException.class})
    public String handleSqlException(Exception e) {
        log.error("数据库错误", e);
        JsonResult result = new JsonResult();
        result.setStatus(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()));
        result.setMessage("数据库错误");
        return JsonUtil.toJson(result);
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({Exception.class})
    public String handleException(Exception e) {
        log.error("服务器内部错误", e);
        JsonResult result = new JsonResult();
        result.setStatus(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()));
        String msg ="服务器内部错误";
        result.setMessage("服务器内部错误");
        return JsonUtil.toJson(result);
    }

    /**
     * token 异常捕获
     *
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({JwtTokenException.class})
    public JsonResult handleJwtTokenException(Exception e) {
        log.error("权限验证异常,详情：" + e.getMessage(), e);
        JsonResult result = new JsonResult();
        result.setStatus(ErrorCode.TOKEN_NO_PASS.getKey());
        result.setMessage("权限验证异常,详情：" + e.getMessage());
        return result;
    }

    /**
     * 业务异常
     *
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({BusinessException.class})
    public JsonResult handleBusinessException(Exception e) {
        log.error("业务异常", e);
        JsonResult result = new JsonResult();
        result.setStatus(ErrorCode.FAIL.getKey());
        result.setMessage(e.getMessage());
        return result;
    }
}
